﻿#include"List.h"

SLTNode* BuySLTNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (newnode != NULL)
	{
		newnode->data = x;
		newnode->next = NULL;
		return newnode;
	}
	else
		perror("malloc");
	return NULL;
		
}

SLTNode* FindTail(SLTNode* phead)
{
	while (phead->next)
	{
		phead = phead->next;
	}
	return phead;
}

void SLTPrint(SLTNode* phead)
{
	while (phead)
	{
		printf("%d ", (phead)->data);
		phead = phead->next;
	}
	printf("NULL\n");
}

void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = BuySLTNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}

	else
	{
		SLTNode* tail = FindTail(*pphead);
		tail->next->next = newnode;
	}

}
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

void SLTPopBack(SLTNode** pphead)
{
	if ((*pphead)->next == NULL)
	{
		free(*pphead);
	}
	else
	{
		SLTNode* tail = FindTail(pphead);
		tail->next = NULL;
	}


}
void SLTPopFront(SLTNode** pphead)
{
	assert(*pphead);

	SLTNode* first = *pphead;
	*pphead = first->next;
	free(first);
	first = NULL;
}


// 
SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
// pos֮ǰ
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
// posλɾ
void SLTErase(SLTNode** pphead, SLTNode* pos);

// pos
void SLTInsertAfter(SLTNode* pos, SLTDataType x);
// posλúɾ
void SLTEraseAfter(SLTNode* pos);


